﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_Quang_Cao : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter ad;
    SqlCommand cmd;
    DataTable dt = new DataTable();
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    string sql = "select * from QUANG_CAO";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
        else
        {

            if (!IsPostBack)
            {
                Hien_Thi();
                if (dt.Rows.Count > 0)
                {
                    txtmacongty.Text = dt.Rows[0]["STT"].ToString();
                    txttencongty.Text = dt.Rows[0]["TenCty"].ToString();
                    txthinhminhhoa.Text = dt.Rows[0]["Hinh_minh_hoa"].ToString();
                    txtduonglink.Text = dt.Rows[0]["Href"].ToString();
                    txtngaybatdau.Text = dt.Rows[0]["Ngay_bat_dau"].ToString();
                    txtngayketthuc.Text = dt.Rows[0]["Ngay_het_han"].ToString();
                }
                LockTexBox(true);
                btnLuu.Visible = false;
                btnKLuu.Visible = false;

            }
        }
    }
    public void Hien_Thi()
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                lblErr.Text = "Không có dữ liệu";
            }
        }
        catch (Exception ex)
        {
            lblErr.Text = "Lỗi: " + ex.Message;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Hien_Thi();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idrow = GridView1.SelectedIndex;
        txtmacongty.Text = GridView1.Rows[idrow].Cells[0].Text;
        txttencongty.Text = GridView1.Rows[idrow].Cells[1].Text;
        txthinhminhhoa.Text = GridView1.Rows[idrow].Cells[2].Text;
        txtduonglink.Text = GridView1.Rows[idrow].Cells[3].Text;
        txtngaybatdau.Text = GridView1.Rows[idrow].Cells[4].Text;
        txtngayketthuc.Text = GridView1.Rows[idrow].Cells[5].Text;
    }
    protected void btnuploadimage_Click(object sender, EventArgs e)
    {
        HttpPostedFile file = FileUpload1.PostedFile;
        if (FileUpload1.HasFile == false || file.ContentLength > 800000)
        {
            lblErrHinh.Text = "Chọn file ảnh khác";
        }
        else
        {
            try
            {
                string strPath = Server.MapPath("~/images/" + FileUpload1.FileName);
                FileUpload1.SaveAs(strPath);
                txthinhminhhoa.Text = "~/images/" + FileUpload1.FileName.ToString();
                lblErrHinh.Text = "Upload thành công";
            }
            catch
            {
                lblErrHinh.Text = "Upload thất bại";
            }
        }
    }
    public void LockTexBox(Boolean t)
    {
        txtduonglink.ReadOnly = t;
        txthinhminhhoa.ReadOnly = t;
        txtmacongty.ReadOnly = t;
        txtngaybatdau.ReadOnly = t;
        txtngayketthuc.ReadOnly = t;
        txttencongty.ReadOnly = t;
    }
    protected void btnthem_Click(object sender, EventArgs e)
    {
        txtmacongty.ReadOnly = true;
        txtduonglink.Text = txthinhminhhoa.Text = txtmacongty.Text = txtngaybatdau.Text = txtngayketthuc.Text = txttencongty.Text = "";
        lblErr.Text = "";
        LockTexBox(false);
        btnthem.Visible = false;
        btnSua.Visible = false;
        btnXoa.Visible = false;
        btnLuu.Visible = true;
        btnKLuu.Visible = true;
        Session["Them"] = 1;
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        LockTexBox(false);
        txtmacongty.ReadOnly = true;
        btnthem.Visible = false;
        btnSua.Visible = false;
        btnXoa.Visible = false;
        btnLuu.Visible = true;
        btnKLuu.Visible = true;
        Session["Them"] = 2;
        txttencongty.Focus();
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            string sql = "";
            cmd = new SqlCommand();
            cmd.Connection = con;
            if (Session["Them"].ToString() == "1")
            {
                sql = "insert into QUANG_CAO (TenCty,Hinh_minh_hoa, Href,Ngay_bat_dau,Ngay_het_han)" + "values(N'" + txttencongty.Text + "',N'" + txthinhminhhoa.Text + "',N'" + txtduonglink.Text + "','" + txtngaybatdau.Text + "','" + txtngayketthuc.Text + "')";
            }
            else if (Session["Them"].ToString() == "2")
            {
                sql = "update QUANG_CAO set TenCty='" + txttencongty.Text + "',Hinh_minh_hoa='" + txthinhminhhoa.Text + "',Href='" + txtduonglink.Text + "',Ngay_bat_dau='" + txtngaybatdau.Text + "',Ngay_het_han='" + txtngayketthuc.Text + "' where STT='" + txtmacongty.Text + "'";
            }
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lblErr.Text = "Thực hiện thành công";
                btnthem.Visible = true;
                btnSua.Visible = true;
                btnXoa.Visible = true;
                btnLuu.Visible = false;
                btnKLuu.Visible = false;
                Hien_Thi();
            }
            else
            {
                lblErr.Text = "Thực hiện không thành công";
            }
            con.Dispose();
            con.Close();
        }
        catch (Exception ex)
        {
            lblErr.Text = "Lỗi: " + ex.Message;
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        con = new SqlConnection(Connect);
        try
        {
        con.Open();
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "delete from QUANG_CAO where STT = '" + txtmacongty.Text + "'";
        int i;
        i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            lblErr.Text = "Đã Xóa Thành Công";
            Hien_Thi();
            if (dt.Rows.Count > 0)
            {
                txtmacongty.Text = dt.Rows[0]["STT"].ToString();
                txttencongty.Text = dt.Rows[0]["TenCty"].ToString();
                txthinhminhhoa.Text = dt.Rows[0]["Hinh_minh_hoa"].ToString();
                txtduonglink.Text = dt.Rows[0]["Href"].ToString();
                txtngaybatdau.Text = dt.Rows[0]["Ngay_bat_dau"].ToString();
                txtngayketthuc.Text = dt.Rows[0]["Ngay_het_han"].ToString();
            }
            else
            {
                lblErr.Text = "Không xóa được";
            }
        }
        con.Dispose();
        con.Close();
        }
        catch (Exception ex)
        {
            lblErr.Text = "Lỗi:" + ex.Message;
        }
    }
    protected void btnKLuu_Click(object sender, EventArgs e)
    {
        btnthem.Visible = true;
        btnXoa.Visible = true;
        btnSua.Visible = true;
        btnLuu.Visible = false;
        btnKLuu.Visible = false;
    }
}
